let openApp = new Vue({
  el: '#openApp',
  data: {
    loading: 1,
    selectedOption: '', // 初始值为空
    currentDate: '',
    load:true,
    options: [
      {value: '1', text: '状态'},
      {value: '2', text: '允许'},
      {value: '3', text: '不允许'}
    ],
    Month: [
      {id: 1, name: '1 个月'},
      {id: 2, name: '2 个月'},
      {id: 3, name: '3 个月'},
      {id: 4, name: '4 个月'},
      {id: 5, name: '5 个月'},
      {id: 6, name: '6 个月'},
      {id: 7, name: '7 个月'},
      {id: 8, name: '8 个月'},
      {id: 9, name: '9 个月'},
      {id: 10, name: '10 个月'},
      {id: 11, name: '11 个月'},
      {id: 12, name: '12 个月'},
    ],
    statusCreate: [], // 创建时间
    video: [], // 视频列表主页
    next: [],  // 下一页
    checkbox: [],  // 是否显示
    isVisible: true,  // 是否显示删除内容
    isMessage: false, // 是否显示消息
    message: '',  // 消息内容 // 消息内容
    movie: [], // 视频信息 视频编辑页面
  },
  methods: {

    // 获取video视频
    openApp: function (pageNum) {
      if (pageNum == null) {
        pageNum = 1;
      }
      $.ajax({
        url: '/web/open/select',
        method: 'GET',
        data: {
          pageNum: pageNum
        },
        success: function (e) {
          console.log("videos{}", e);
          if (e.code == OK) {
            openApp.load=false;
            console.log("videos{}", e.data)
            openApp.next = e.data;
            openApp.video = e.data.list;
            openApp.LocaldateTime(openApp.video);
            for (let i = 0; i < openApp.video.length; i++) {
              console.log("videos{}", openApp.video[i].status);
              if (openApp.video[i].status == 0) {
                openApp.video[i].status = "非会员";
              } else {
                openApp.video[i].status = "会员";
              }
            }

          }
        }
      })
    },
    // 问题持续时间
    LocaldateTime: function (questions) {
      for (let i = 0; i < questions.length; i++) {
        //获取创建时间
        let createTime = new Date(questions[i].statusCreate).getTime();
        // console.log("创建时间{}", createTime);
        //获取当前时间
        let now = new Date().getTime();
        // console.log("当前时间{}", now);
        //计算时间差
        let time = createTime - now;
        //console.log("时间差{}", time);
        if (time < 0) {
          questions[i].time = "已过期";
          openApp.deleteStatus(questions[i].id, 0);
        } else if (time < 1000 * 60 * 60) {
          questions[i].time = parseInt(time / (1000 * 60)) + "分钟";
        } else if (time < 1000 * 60 * 60 * 24) {
          questions[i].time = parseInt(time / (1000 * 60 * 60)) + "小时";
        } else {
          questions[i].time = parseInt(time / (1000 * 60 * 60 * 24)) + "天";
        }
      }
    },
    // 提示框
    onmessage: function (message) {
      openApp.message = message;
      openApp.isMessage = true;
      setTimeout(function () {
        openApp.isMessage = false;
      }, 2000);
    },
    // 通过ID获取视频 进行修改
    handleInput: function (e) {
      var value = e.target.value;
      setTimeout(() => {
        console.log('Input changed:', value);
        $.ajax({
          url: '/web/open/selectById',
          method: 'GET',
          data: {
            id: value,
          },
          success: function (e) {
            if (e.code == OK) {
              console.log("videos{}", e.data);
              openApp.movie = e.data;
              openApp.dateTime(openApp.movie);
              if (openApp.movie.status == 0) {
                openApp.movie.status = "非会员";
              } else {
                openApp.movie.status = "会员";
              }
            } else {
              openApp.onmessage("该用户不存在");
            }

          }
        })
      }, 1000);

    },
    // 问题持续时间
    dateTime: function (questions) {
      //获取创建时间
      let createTime = new Date(questions.statusCreate).getTime();
      // console.log("创建时间{}", createTime);
      //获取当前时间
      let now = new Date().getTime();
      // console.log("当前时间{}", now);
      //计算时间差
      let time = createTime - now;
      //console.log("时间差{}", time);
      if (time < 0) {
        questions.time = "已过期";
        openApp.deleteStatus(questions.id, 0);

      } else if (time < 1000 * 60 * 60) {
        questions.time = parseInt(time / (1000 * 60)) + "分钟";
      } else if (time < 1000 * 60 * 60 * 24) {
        questions.time = parseInt(time / (1000 * 60 * 60)) + "小时";
      } else {
        questions.time = parseInt(time / (1000 * 60 * 60 * 24)) + "天";
      }
    },
    // 获取视频信息// 修改提交表单的新数据
    handleSubmit: function (e) {
      this.loading = 1;
      // 在这里处理提交逻辑
      console.log('提交的数据movie:', this.movie);
      let id = this.movie.id;
      let time = this.statusCreate;
      console.log("time{} id{}", time, id);
      // time个月后的时间戳
      // 当前时间
      const now = new Date();
      // 创建一个新的日期对象，增加一个月
      const oneMonthLater = new Date(now);
      oneMonthLater.setMonth(now.getMonth() + time);
      console.log("time个月后的时间戳是：" + oneMonthLater.getTime());
      $.ajax({
        url: '/web/open/update',
        method: 'POST',
        data: {
          id: id,
          status: 1,
          statusCreate: oneMonthLater.getTime()
        },
        success: function (res) {
          console.log("res{}", res);
          if (res.code == OK) {
            openApp.loading = 0;
            openApp.onmessage("充值成功");
            openApp.movie = [];
            openApp.statusCreate = '';
          } else {
            openApp.onmessage("充值失败");
          }
        },
        error: function (e) {
          console.log("修改失败");
          openApp.onmessage("修改失败");
        }
      })
    },
    // 身份变更
    deleteStatus: function (id, status) {
      $.ajax({
        url: '/web/open/deleteStatus',
        method: 'POST',
        data: {
          id: id,
          status: status
        },
        success: function (res) {
        }
      })
    },
    // 获取当前时间
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    updateDate() {
      const now = new Date();
      this.currentDate = this.formatDate(now);
    }

  }, created: function () {
    // 初始化日期
    this.updateDate();
    // 每秒更新时间
    this.interval = setInterval(this.updateDate, 1000);
    this.openApp();
  },
  beforeDestroy() {
    // 清除定时器
    clearInterval(this.interval);
  }
})